﻿// TomatoConfigDialog.cpp
#include "TomatoConfigDialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QFileDialog>
#include <QMediaPlayer>
#include <QSoundEffect>
#include <QMessageBox>
#include <QDir>
#include <QStandardPaths>
#include "toolBox.h"

TomatoConfigDialog::TomatoConfigDialog(QWidget* parent)
    : QDialog(parent)
{
    setupUI();
    setupConnections();
    loadCurrentSettings();

    setWindowTitle("番茄钟设置");
    setMinimumSize(600, 700);
}

void TomatoConfigDialog::setupUI()
{
    QVBoxLayout* mainLayout = new QVBoxLayout(this);

    // === 声音设置 ===
    m_soundGroup = new QGroupBox("声音设置");
    QFormLayout* soundLayout = new QFormLayout(m_soundGroup);

    // 工作完成声音
    QHBoxLayout* workSoundLayout = new QHBoxLayout();
    m_workSoundCombo = new QComboBox();
    m_workSoundCombo->addItems({ "默认提示音", "风铃", "钟声", "鸟鸣", "自定义..." });
    m_browseWorkSoundBtn = new QPushButton("浏览");
    m_playWorkSoundBtn = new QPushButton("试听");
    workSoundLayout->addWidget(m_workSoundCombo);
    workSoundLayout->addWidget(m_browseWorkSoundBtn);
    workSoundLayout->addWidget(m_playWorkSoundBtn);

    // 休息完成声音
    QHBoxLayout* restSoundLayout = new QHBoxLayout();
    m_restSoundCombo = new QComboBox();
    m_restSoundCombo->addItems({ "默认提示音", "轻柔铃声", "水滴声", "海浪声", "自定义..." });
    m_browseRestSoundBtn = new QPushButton("浏览");
    m_playRestSoundBtn = new QPushButton("试听");
    restSoundLayout->addWidget(m_restSoundCombo);
    restSoundLayout->addWidget(m_browseRestSoundBtn);
    restSoundLayout->addWidget(m_playRestSoundBtn);

    // 背景声音
    QHBoxLayout* bgSoundLayout = new QHBoxLayout();
    m_bgSoundCombo = new QComboBox();
    m_bgSoundCombo->addItems({ "无背景音", "雨声", "白噪音", "森林", "咖啡馆", "自定义..." });
    m_browseBgSoundBtn = new QPushButton("浏览");
    m_playBgSoundBtn = new QPushButton("试听");
    bgSoundLayout->addWidget(m_bgSoundCombo);
    bgSoundLayout->addWidget(m_browseBgSoundBtn);
    bgSoundLayout->addWidget(m_playBgSoundBtn);

    // 音量控制
    QHBoxLayout* volumeLayout = new QHBoxLayout();
    m_volumeLabel = new QLabel("音量:");
    m_volumeSlider = new QSlider(Qt::Horizontal);
    m_volumeSlider->setRange(0, 100);
    m_volumeSlider->setValue(80);
    volumeLayout->addWidget(m_volumeLabel);
    volumeLayout->addWidget(m_volumeSlider);

    soundLayout->addRow("工作完成声音:", workSoundLayout);
    soundLayout->addRow("休息完成声音:", restSoundLayout);
    soundLayout->addRow("背景声音:", bgSoundLayout);
    soundLayout->addRow(volumeLayout);

    // === 外观设置 ===
    m_appearanceGroup = new QGroupBox("外观设置");
    QFormLayout* appearanceLayout = new QFormLayout(m_appearanceGroup);

    QHBoxLayout* bgImageLayout = new QHBoxLayout();
    m_bgImageCombo = new QComboBox();
    m_bgImageCombo->addItems({ "默认背景", "森林", "海滩", "星空", "简约", "自定义..." });
    m_browseBgImageBtn = new QPushButton("浏览");
    m_previewBgImageBtn = new QPushButton("预览");
    bgImageLayout->addWidget(m_bgImageCombo);
    bgImageLayout->addWidget(m_browseBgImageBtn);
    bgImageLayout->addWidget(m_previewBgImageBtn);

    m_previewLabel = new QLabel();
    m_previewLabel->setFixedSize(200, 120);
    m_previewLabel->setStyleSheet("border: 1px solid #ccc; background: #f5f5f5;");
    m_previewLabel->setAlignment(Qt::AlignCenter);
    m_previewLabel->setText("背景预览");

    appearanceLayout->addRow("背景图片:", bgImageLayout);
    appearanceLayout->addRow(m_previewLabel);

    // === 提醒设置 ===
    m_reminderGroup = new QGroupBox("提醒设置");
    QVBoxLayout* reminderLayout = new QVBoxLayout(m_reminderGroup);

    m_desktopNotificationCheck = new QCheckBox("启用桌面通知");
    m_soundNotificationCheck = new QCheckBox("启用声音提醒");
    m_vibrationCheck = new QCheckBox("启用震动提醒");
    m_skipRestCheck = new QCheckBox("允许跳过休息");

    reminderLayout->addWidget(m_desktopNotificationCheck);
    reminderLayout->addWidget(m_soundNotificationCheck);
    reminderLayout->addWidget(m_vibrationCheck);
    reminderLayout->addWidget(m_skipRestCheck);

    // === 时间设置 ===
    m_timeGroup = new QGroupBox("时间设置");
    QFormLayout* timeLayout = new QFormLayout(m_timeGroup);

    m_workDurationSpin = new QSpinBox();
    m_workDurationSpin->setRange(1, 60);
    m_workDurationSpin->setValue(25);
    m_workDurationSpin->setSuffix(" 分钟");

    m_restDurationSpin = new QSpinBox();
    m_restDurationSpin->setRange(1, 30);
    m_restDurationSpin->setValue(5);
    m_restDurationSpin->setSuffix(" 分钟");

    timeLayout->addRow("工作时间:", m_workDurationSpin);
    timeLayout->addRow("休息时间:", m_restDurationSpin);

    // === 按钮 ===
    QHBoxLayout* buttonLayout = new QHBoxLayout();
    m_okButton = new QPushButton("确定");
    m_applyButton = new QPushButton("应用");
    m_resetButton = new QPushButton("恢复默认");
    m_cancelButton = new QPushButton("取消");

    buttonLayout->addWidget(m_resetButton);
    buttonLayout->addStretch();
    buttonLayout->addWidget(m_okButton);
    buttonLayout->addWidget(m_applyButton);
    buttonLayout->addWidget(m_cancelButton);

    // 添加到主布局
    mainLayout->addWidget(m_soundGroup);
    mainLayout->addWidget(m_appearanceGroup);
    mainLayout->addWidget(m_reminderGroup);
    mainLayout->addWidget(m_timeGroup);
    mainLayout->addLayout(buttonLayout);
}

void TomatoConfigDialog::setupConnections()
{
    connect(m_browseWorkSoundBtn, &QPushButton::clicked, this, &TomatoConfigDialog::browseWorkSound);
    connect(m_browseRestSoundBtn, &QPushButton::clicked, this, &TomatoConfigDialog::browseRestSound);
    connect(m_browseBgSoundBtn, &QPushButton::clicked, this, &TomatoConfigDialog::browseBackgroundSound);
    connect(m_browseBgImageBtn, &QPushButton::clicked, this, &TomatoConfigDialog::browseBackgroundImage);

    connect(m_playWorkSoundBtn, &QPushButton::clicked, this, &TomatoConfigDialog::playWorkSound);
    connect(m_playRestSoundBtn, &QPushButton::clicked, this, &TomatoConfigDialog::playRestSound);
    connect(m_playBgSoundBtn, &QPushButton::clicked, this, &TomatoConfigDialog::playBackgroundSound);
    connect(m_previewBgImageBtn, &QPushButton::clicked, this, &TomatoConfigDialog::previewBackgroundImage);

    connect(m_okButton, &QPushButton::clicked, this, &TomatoConfigDialog::accept);
    connect(m_applyButton, &QPushButton::clicked, this, &TomatoConfigDialog::applySettings);
    connect(m_resetButton, &QPushButton::clicked, this, &TomatoConfigDialog::resetToDefault);
    connect(m_cancelButton, &QPushButton::clicked, this, &TomatoConfigDialog::reject);
}

void TomatoConfigDialog::loadCurrentSettings()
{
    // 这里应该从配置文件或数据库中加载当前设置
    // 暂时设置默认值
    m_workSoundCombo->setCurrentIndex(0);
    m_restSoundCombo->setCurrentIndex(0);
    m_bgSoundCombo->setCurrentIndex(0);
    m_bgImageCombo->setCurrentIndex(0);
    m_desktopNotificationCheck->setChecked(true);
    m_soundNotificationCheck->setChecked(true);
    m_vibrationCheck->setChecked(false);
    m_skipRestCheck->setChecked(true);
}

// 实现各个功能函数...
void TomatoConfigDialog::browseWorkSound()
{
    QString file = QFileDialog::getOpenFileName(this, "选择工作完成提示音",
        QStandardPaths::writableLocation(QStandardPaths::MusicLocation),
        "音频文件 (*.mp3 *.wav *.ogg)");
    if (!file.isEmpty()) {
        m_workSoundCombo->setCurrentText("自定义文件");
        m_currentWorkSound = file;
    }
}

void TomatoConfigDialog::browseRestSound()
{
    QString file = QFileDialog::getOpenFileName(this, "选择休息完成提示音",
        QStandardPaths::writableLocation(QStandardPaths::MusicLocation),
        "音频文件 (*.mp3 *.wav *.ogg)");
    if (!file.isEmpty()) {
        m_restSoundCombo->setCurrentText("自定义文件");
        m_currentRestSound = file;
    }
}

void TomatoConfigDialog::browseBackgroundSound()
{
    QString file = QFileDialog::getOpenFileName(this, "选择背景音乐",
        QStandardPaths::writableLocation(QStandardPaths::MusicLocation),
        "音频文件 (*.mp3 *.wav *.ogg)");
    if (!file.isEmpty()) {
        m_bgSoundCombo->setCurrentText("自定义文件");
        m_currentBgSound = file;
    }
}

void TomatoConfigDialog::browseBackgroundImage()
{
    QString file = QFileDialog::getOpenFileName(this, "选择背景图片",
        QStandardPaths::writableLocation(QStandardPaths::PicturesLocation),
        "图片文件 (*.png *.jpg *.jpeg *.bmp)");
    if (!file.isEmpty()) {
        m_bgImageCombo->setCurrentText("自定义文件");
        m_currentBgImage = file;

        // 立即预览
        QPixmap pixmap(file);
        if (!pixmap.isNull()) {
            m_previewLabel->setPixmap(pixmap.scaled(m_previewLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }
    }
}

void TomatoConfigDialog::playWorkSound()
{
    // 实现播放工作提示音
   // QSoundEffect::play();
}

void TomatoConfigDialog::playRestSound()
{
    // 实现播放休息提示音
   // QSoundEffect::play();
}

void TomatoConfigDialog::playBackgroundSound()
{
    // 实现播放背景音乐
}

void TomatoConfigDialog::previewBackgroundImage()
{
    QString currentBg = m_bgImageCombo->currentText();
    if (currentBg == "自定义文件" && !m_currentBgImage.isEmpty()) {
        QPixmap pixmap(m_currentBgImage);
        if (!pixmap.isNull()) {
            m_previewLabel->setPixmap(pixmap.scaled(m_previewLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }
    }
    else {
        // 显示内置背景预览
        m_previewLabel->setText(QString("预览: %1").arg(currentBg));
    }
}

void TomatoConfigDialog::applySettings()
{
    // 保存设置到配置文件
    QMessageBox::information(this, "提示", "设置已应用！");
    emit settingsApplied();
}

void TomatoConfigDialog::resetToDefault()
{
    int ret = QMessageBox::question(this, "确认", "确定要恢复默认设置吗？");
    if (ret == QMessageBox::Yes) {
        m_workSoundCombo->setCurrentIndex(0);
        m_restSoundCombo->setCurrentIndex(0);
        m_bgSoundCombo->setCurrentIndex(0);
        m_bgImageCombo->setCurrentIndex(0);
        m_desktopNotificationCheck->setChecked(true);
        m_soundNotificationCheck->setChecked(true);
        m_vibrationCheck->setChecked(false);
        m_skipRestCheck->setChecked(true);
        m_workDurationSpin->setValue(25);
        m_restDurationSpin->setValue(5);
        m_volumeSlider->setValue(80);

        m_currentWorkSound.clear();
        m_currentRestSound.clear();
        m_currentBgSound.clear();
        m_currentBgImage.clear();
        m_previewLabel->setText("背景预览");
    }
}

// Getter 方法实现
QString TomatoConfigDialog::getWorkSound() const {
    return m_workSoundCombo->currentText() == "自定义文件" ? m_currentWorkSound : m_workSoundCombo->currentText();
}

QString TomatoConfigDialog::getRestSound() const {
    return m_restSoundCombo->currentText() == "自定义文件" ? m_currentRestSound : m_restSoundCombo->currentText();
}

QString TomatoConfigDialog::getBackgroundSound() const {
    return m_bgSoundCombo->currentText() == "自定义文件" ? m_currentBgSound : m_bgSoundCombo->currentText();
}

QString TomatoConfigDialog::getBackgroundImage() const {
    return m_bgImageCombo->currentText() == "自定义文件" ? m_currentBgImage : m_bgImageCombo->currentText();
}

bool TomatoConfigDialog::isDesktopNotificationEnabled() const { return m_desktopNotificationCheck->isChecked(); }
bool TomatoConfigDialog::isSoundEnabled() const { return m_soundNotificationCheck->isChecked(); }
bool TomatoConfigDialog::isVibrationEnabled() const { return m_vibrationCheck->isChecked(); }
bool TomatoConfigDialog::isSkipRestEnabled() const { return m_skipRestCheck->isChecked(); }
int TomatoConfigDialog::getWorkDuration() const { return m_workDurationSpin->value(); }
int TomatoConfigDialog::getRestDuration() const { return m_restDurationSpin->value(); }
int TomatoConfigDialog::getVolume() const { return m_volumeSlider->value(); }